package com.wephone.Student.ui;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.wephone.Student.R;
import com.wephone.Student.bean.ZhidaoAskInfoResponse;
import com.wephone.Student.bean.data.ZhidaoCommentData;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.RoundImageView;

public class ZhidaoCommentListActivity extends BaseActivity {
	private ListView listView;
	private ListViewAdapter adapter;
	private String qId;
	private String cId;
	private List<ZhidaoCommentData> results = new ArrayList<ZhidaoCommentData>();
	
	private MyResponseHandler getHandler(){
		MyResponseHandler handler = new MyResponseHandler(ZhidaoCommentListActivity.this,ZhidaoAskInfoResponse.class){
			@Override
			public void onSuccess(String result) {
				super.onSuccess(result);
				
				if(getDataSuccess){
					ZhidaoAskInfoResponse res = (ZhidaoAskInfoResponse)response;
					
					if(res.getData().getList()==null || adapter.pageIdx>=res.getData().getList().size() || res.getData().getList().size()<Constants.PAGE_SIZE){
						adapter.hasMore = false;
					}else{
						adapter.pageIdx++;
					}
					if(res.getData().getList()!=null){
						results.addAll(res.getData().getList());
						setResult(results.size());
						adapter.notifyDataSetChanged();
					}
				}
			}
		};
		return handler;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode==1 && resultCode==11){
			results.clear();
			adapter.hasMore = true;
			adapter.pageIdx = 1;
			
			RequestParams params = new RequestParams();
			params.put("id", cId);
			params.put("Page", "1");
			params.put("PageSize", ""+Constants.PAGE_SIZE);
			HttpClient.getInstance().get(Constants.API_ZHIDAO_ASK_REPLY_LIST_URL, params, getHandler());
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zhi_dao_comment_list);
		
		init();
	}
	
	private void init(){
		listView = (ListView)findViewById(R.id.listview);
		adapter = new ListViewAdapter();
		listView.setAdapter(adapter);
		
		findViewById(R.id.btn_back).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		findViewById(R.id.btn_top_right).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Bundle bundle = new Bundle();
				bundle.putString("qId", qId);
				bundle.putString("cId", cId);
				startActivityForResult(new Intent(getApplicationContext(),ZhidaoAddCommentActivity.class).putExtras(bundle),1);
			}
		});
		
		Bundle bundle = getIntent().getExtras();
		if(bundle!=null){
			qId = bundle.getString("qId");
			cId = bundle.getString("cId");
			
			RequestParams params = new RequestParams();
			params.put("id", cId);
			params.put("Page", "1");
			params.put("PageSize", ""+Constants.PAGE_SIZE);
			HttpClient.getInstance().get(Constants.API_ZHIDAO_ASK_REPLY_LIST_URL, params, getHandler());
		}
	}
	
	private class ListViewAdapter extends BaseAdapter{
		public int pageIdx = 1;
		public boolean hasMore = true;
		private SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
		
		@Override
		public int getCount() {
			return results.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if(convertView==null){
				convertView = getLayoutInflater().inflate(R.layout.include_zhi_dao_comment_list_item, null);
			}
			ZhidaoCommentData commData = results.get(position);
			TextView itemName = (TextView)convertView.findViewById(R.id.item_answer_name);
			RoundImageView itemImage = (RoundImageView)convertView.findViewById(R.id.item_image);
			if(commData.getAnonymous()!=null && commData.getAnonymous().equals("1")){
				itemImage.setImageResource(R.drawable.default_user_logo);
				itemName.setText("匿名");
			}else{
				itemName.setText(commData.getuInfo().getuName());
				if(!TextUtils.isEmpty(commData.getuInfo().getuIcon())){
					DisplayImageOptions options = new DisplayImageOptions.Builder()
			        .showStubImage(R.drawable.default_user_logo)
			        .showImageOnFail(R.drawable.default_user_logo)
			        .bitmapConfig(Bitmap.Config.ARGB_8888)
			        .cacheInMemory()
			        .cacheOnDisc()
//			        .displayer(new RoundedBitmapDisplayer(1000))
			        .build();
					String endStr = commData.getuInfo().getuIcon().substring(commData.getuInfo().getuIcon().lastIndexOf("."));
					imageLoader.displayImage(Constants.IMAGE_PHOTO_SERVER_PATH+commData.getuInfo().getuIcon().replace(endStr, "_150_150"+endStr), itemImage, options);
				}else{
					itemImage.setImageResource(R.drawable.default_user_logo);
				}
			}
			TextView itemTime = (TextView)convertView.findViewById(R.id.item_answer_time);
			itemTime.setText(sdf.format(new Date(Long.parseLong(commData.getCtime())*1000)));
			TextView itemContent = (TextView)convertView.findViewById(R.id.item_answer_content);
			itemContent.setText(commData.getContent());
			//load more
			if(position>=results.size()-1 && hasMore){
				RequestParams params = new RequestParams();
				params.put("id", cId);
				params.put("Page", ""+pageIdx);
				params.put("PageSize", ""+Constants.PAGE_SIZE);
				HttpClient.getInstance().get(Constants.API_ZHIDAO_ASK_REPLY_LIST_URL, params, getHandler());
			}
			return convertView;
		}		
	}

}
